<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Friendlink extends Controller_Admin_Based
{

    public function action_add()
    {
        if ($_POST)
        {
            $ad = Ad::instances()->set($_POST);
            if ($ad)
            {
                Message::set('链接添加成功');
                Request::current()->redirect('/admin/friendlink/add');
            }
            else
            {
                Message::set('链接添加失败');
                Request::current()->redirect('/admin/friendlink/add', 'error');
            }
        }
        
        
        $pagination_arr = Paginationhelp::get_data('ad', 'id_ad');
        $data = array(
            'result' => $pagination_arr['data'],
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/friendlink_add', $data);
        $this->response->body($view);
    }
    
    
    
    public function action_edit() {
        $id_ad = $this->request->param('id');
        $ads = Ad::instances($id_ad)->get();
        if ($_POST)
        {
            $ad = Ad::instances($id_ad)->update($_POST);
            if ($ad)
            {
                Message::set('链接修改成功');
                Request::current()->redirect('/admin/friendlink/add');
            }
            else
            {
                Message::set('链接修改失败', 'error');
                Request::current()->redirect('/admin/friendlink/add');
            }
        }
        
      
        $pagination_arr = Paginationhelp::get_data('ad', 'id_ad');
        $data = array(
            'result' => $pagination_arr['data'],
            'data'=>$ads,
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/friendlink_edit', $data);
        $this->response->body($view);
    }
    
        public function action_delete() {
        $id_ad = $this->request->param('id');
          $flag =Ad::instances($id_ad)->delete();
        if ($flag) {
            Message::set('链接删除成功');
            Request::current()->redirect('admin/friendlink/add');
        } else {
            Message::set('链接删除失败', 'error');
            Request::current()->redirect('/admin/friendlink/add');
        }
    }

}